package com.taxer.controller;

import com.taxer.contants.Constants;
import com.taxer.db.entity.Admin;
import com.taxer.db.entity.Company;
import com.taxer.exception.TaxerExcepiton;
import com.taxer.result.Result;
import com.taxer.service.AdminService;
import com.taxer.service.CompanyService;
import com.taxer.service.impl.CompanyServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by gx on 2017/9/15.
 */
@Controller
@RequestMapping(value = "/company")
public class CompanyController {
    @Resource
    private CompanyService companyService;

    @RequestMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Company company) {
        Result<Integer> result = new Result<>();
        try {
            companyService.addCompany(company);
            result.setMessage("添加成功！");
            result.setData(1);
        } catch (TaxerExcepiton taxerExcepiton) {
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
    @RequestMapping("/selectAll")
    @ResponseBody
    public  Result selectAll(){
        Result<List<Company>> result = new Result<>();
        try {
            result.setMessage("查询成功！");
            List<Company> coms = companyService.selectAllCompany();
            for (Company c:coms
                 ) {
                System.out.println(c.getAddress());
            }
            result.setData(coms);
        } catch (TaxerExcepiton taxerExcepiton) {
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public  Result selectById(@RequestBody Company company){
        Result<Company> result = new Result<>();
        try {
            result.setMessage("查询成功！");
            result.setData(companyService.findCompany(company));
        } catch (TaxerExcepiton taxerExcepiton) {
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public  Result delete(@RequestBody Company company){
        Result<Integer> result = new Result<>();
        try {
            result.setMessage("删除成功！");
            companyService.deleteCompany(company);
            result.setData(1);
        } catch (TaxerExcepiton taxerExcepiton) {
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
    @RequestMapping("update")
    @ResponseBody
    public  Result update(@RequestBody Company company){
        Result result = new Result();
        try {
            System.out.println(company);
            result.setMessage("修改成功！");
            result.setData(companyService.updateCompany(company));
        } catch (TaxerExcepiton taxerExcepiton) {
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
}

